import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import AboutView from '@/views/AboutView.vue'
import UserList from '@/views/User/UserList.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    component: AboutView
  },
  {
    path: '/users',
    name: 'users',
    component: UserList,
    meta: { requiresAuth: true } // 路由元信息
  },
  {
    path: '/users/:id',
    name: 'user-detail',
    component: () => import('@/views/User/UserDetail.vue'), // 懒加载
    props: true
  }
]

const router = createRouter({
  // hash模式：createWebHashHistory，history模式：createWebHistory
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫示例
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !localStorage.getItem('token')) {
    next({ name: 'login' })
  } else {
    next()
  }
})

export default router